package com.kpijavatraining6.module2.questions;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Vladislav
 * Date: 07.11.13
 * Time: 23:36
 * To change this template use File | Settings | File Templates.
 */
public class CheckAnswerQuestion extends QuestionStructure {
    private ArrayList<String> answers;
    private String rightAnswer;

    public CheckAnswerQuestion(String newQuestion,String newRightAnswer, int questionLevel, /*Topic topic,*/ String ... newAnswers) {
        answers = new ArrayList<String>();
        question = newQuestion;
        rightAnswer = newRightAnswer;
        for ( String answer : newAnswers) {
            answers.add(answer);
        }
        this.topic = topic;
        this.questionLevel = questionLevel;
    }

    public boolean isRightAnswer(String userAnswer) {
        if (rightAnswer.equals(userAnswer)) {
            return true;
        }
        return false;
    }

    public void printQuestion() {
        System.out.println("Choose the correct answer and enter appropriate number of variant:");
        int i = 1;
        System.out.println(question);
        for(String answer : answers) {
            System.out.printf("%d) %s\n", i, answer);
            i++;
        }
    }

    public void printToFile(BufferedWriter bufferedWriter, String userAnswer) {
       try{
           bufferedWriter.write("1 "+question+" ");
           for (String answer: answers) {
               bufferedWriter.write(answer+";");
           }
           bufferedWriter.write(" "+rightAnswer+" "+userAnswer);
       }   catch (IOException e) {
               System.out.println("an error occurred while saving data");
           }
    }

    public void printToFileOnlyQuestion(BufferedWriter bufferedWriter) {
        try{
            bufferedWriter.write("1 "+question+" ");
            for (String answer: answers) {
                bufferedWriter.write(answer+";");
            }
            bufferedWriter.write(" "+rightAnswer);
        }   catch (IOException e) {
            System.out.println("an error occurred while saving data");
        }
    }
}
